<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
// import resize from './mixins/resize'
import { getLastIncome } from '../../api/welcome'
export default {
//   mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    // chartData: {
    //   type: Object,
    //   required: true
    // }
  },
  data() {
    return {
      chart: null,
      chartData: '',
      recentDay: [],
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val,this.recentDay)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  created() {
    this.getLastIncomeData();
    this.getRecentDay();
  },
  methods: {
    getLastIncomeData(){
        getLastIncome().then(res=>{
          this.chartData = res.data
        })
      },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData,this.recentDay)
      
    },
    getRecentDay() {
	    let arr = []
	    for(let i = 6;i >= 0;i --) {
	    	let temp = this.getDay(-i)
    		arr.push(temp)
    	}
    	this.recentDay = arr
      console.log(this.recentDay)
    },
    getDay(day){
       var today = new Date();
       var targetday_milliseconds = today.getTime() + 1000*60*60*24*day;
       today.setTime(targetday_milliseconds); 
       var tMonth = today.getMonth();
       var tDate = today.getDate();
       tMonth = this.doHandleMonth(tMonth + 1);
       tDate = this.doHandleMonth(tDate);
       return tMonth + "-" + tDate;
    },
 doHandleMonth(month){
    var m = month;
    if(month.toString().length == 1){
     m = "0" + month;
    }
    return m;
    },
    setOptions({ expenses, income } = {},date) {
      this.chart.setOption({
        xAxis: {
          data: date,
          //  data: [date[0],date[1],date[2],date[3],date[4],date[5],date[6]],
          boundaryGap: true,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
        //   data: ['expected', 'actual']
        data: ['总营业额','住房营业额']
        },
        series: [{
          name: '总营业额',
           itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: income,
          // data: [820, 932, 901, 934, 1290, 1330, 1320],
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '住房营业额',
          smooth: true,
          type: 'bar',
          barWidth: 30,
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          // data: [540,530,525,730,740,760,330],
          data: expenses,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }
        ]
      })
    }
  }
}
</script>